﻿Imports System.Data.OleDb
Imports BUS
Imports DTO
Public Class frmLapHoaDon

    Private hdBus As New HoaDonBUS()
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Public Function KiemTraDieuKienNhap()
        Dim kq As String = ""
        If txtTenhoadon.Text.Trim = "" Then
            kq = kq & "Chưa nhập tên hoa đơn" & vbCrLf
       
        End If
        If txtNgaylap.Text.Trim = "" Then
            kq = kq & "Chưa nhập ngày lập" & vbCrLf
        
        End If
        
        Return kq
    End Function
    'Click Them
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String
        str = MsgBox("Bắt đầu thêm mới dữ liệu - Mời bạn nhấp chuột nhập liệu", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then
            Me.txtMahd.Text = ""
            Me.txtTenhoadon.Text = ""
            Me.txtNgaylap.Text = ""

            Me.txtTongtien.Text = ""
            Me.txtMahd.Enabled = False
            Me.txtTongtien.Enabled = False
            Me.Button3.Visible = True
            Me.Button1.Visible = False

        End If
    End Sub

    Private Sub dtgvDanhSachHoaDon_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgvDanhSachHoaDon.CellContentClick

    End Sub

    Private Sub frmLapHoaDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
        '-----------
        DuaDuLieuVaoComboBox()
        Me.Button3.Visible = False
        Me.btnDongy.Visible = False

    End Sub
    Private Sub LoadDuLieu()
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select * from HoaDon"
        Dim da As OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim cmb As New OleDbCommandBuilder(da)

        Dim DataSet1 As New DataSet()
        da.Fill(DataSet1, "TestDB")
        dtgvDanhSachHoaDon.DataSource = DataSet1.Tables("TestDB")

    End Sub

    ' Phan co ban ket noi

    Protected Function ConnectionData() As OleDbConnection
        Dim strCn As String
        strCn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = QLKS.mdb"
        Dim cn As New OleDbConnection(strCn)
        cn.Open()
        Return cn
    End Function

    Private Sub dtgvDanhSachHoaDon_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvDanhSachHoaDon.SelectionChanged
        'this.textBox_maTTYT.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        'this.textBox_tenTTYT.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        'this.textBox_diachiTTYT.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        Try

            Me.txtMahd.Text = Me.dtgvDanhSachHoaDon.CurrentRow.Cells(0).Value
            Me.txtTenhoadon.Text = Me.dtgvDanhSachHoaDon.CurrentRow.Cells(1).Value
            Me.txtNgaylap.Text = Me.dtgvDanhSachHoaDon.CurrentRow.Cells(3).Value.ToString

            Me.cboMapt.SelectedValue = Me.dtgvDanhSachHoaDon.CurrentRow.Cells(2).Value
            Me.txtTongtien.Text = Me.dtgvDanhSachHoaDon.CurrentRow.Cells(4).Value

        Catch ex As Exception

        End Try
    End Sub

    Private Sub DuaDuLieuVaoComboBox()
        ' nhap nhap chon csdl
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select * from PhieuThue"
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim ds As New DataSet
        da.Fill(ds, "PhieuThue")
        With cboMapt
            .DataSource = ds.Tables("PhieuThue")
            .ValueMember = "MAPHIEUTHUE"
            .DisplayMember = "MAPHIEUTHUE"
        End With

        'Me.cboMapt.DataSource = ds.Tables(0).DefaultView

        ' Me.cboMapt.DisplayMember = "MAPHIEUTHUE"
        ' Me.cboMapt.ValueMember = "MAPHIEUTHUE"


    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub
    'Them Hoa Don - Nut Luu
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim chon_loi As String = KiemTraDieuKienNhap()
        If chon_loi = "" Then
            Dim dt As New DataTable
            dt = hdBus.LayBang()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("TenHoaDon") = txtTenhoadon.Text
            dr("MaPhieuThue") = Me.cboMapt.SelectedValue
            dr("NgayLapHoaDon") = txtNgaylap.Text
            dr("TongTien") = TinhTongTien()
            hdBus.ThemHoaDonBUS(dr)
            MessageBox.Show("Thêm thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtMahd.Enabled = True
            Me.txtTongtien.Enabled = True
            Me.Button3.Visible = False
            Me.Button1.Visible = True
            LoadDuLieu()
        Else
            MessageBox.Show(chon_loi, "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If


    End Sub
    Private Function TinhTongTien()
        Dim tongtien As String = 0
        ' tinh tong tien 
        ' tong tien = ( so ngay thue * don gia )*phuthu(danh cho khach...)
        Dim ngaylaphoadon = txtNgaylap.Text
        Dim maphieuthue = cboMapt.SelectedValue
        tongtien = hdBus.TinhTienPhongBUS(ngaylaphoadon, maphieuthue)

        Return tongtien
    End Function

  

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim str As String
        str = MsgBox("Xóa dữ liệu ?", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then
            hdBus.XoaHoaDonBUS(txtMahd.Text)
            MessageBox.Show("Xóa thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadDuLieu()
        End If


    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim str As String
        str = MsgBox("Sửa dữ liệu?", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then
         
            Me.txtMahd.Enabled = False
            Me.btnDongy.Visible = True
            Me.btnSua.Visible = False

        End If
    End Sub

    Private Sub btnDongy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDongy.Click
       
        Dim chon_loi As String = KiemTraDieuKienNhap()
        If chon_loi = "" Then
            Dim dt As New DataTable
            dt = hdBus.LayBang()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("MaHoaDon") = txtMahd.Text
            dr("TenHoaDon") = txtTenhoadon.Text
            dr("MaPhieuThue") = Me.cboMapt.SelectedValue
            dr("NgayLapHoaDon") = txtNgaylap.Text
            dr("TongTien") = txtTongtien.Text

            hdBus.SuaHoaDonBUS(dr)
            Me.txtMahd.Enabled = True
            Me.btnDongy.Visible = False
            Me.btnSua.Visible = True
            MessageBox.Show("Sửa thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadDuLieu()
        End If

    End Sub
End Class